package com.daolion.utils.log;

import android.content.Context;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.util.Log;

import com.baidubce.BceClientException;
import com.baidubce.BceServiceException;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.bos.BosClient;
import com.baidubce.services.bos.BosClientConfiguration;
import com.baidubce.services.bos.model.PutObjectResponse;
import com.ll.base.R;

import java.io.File;


public class LogUploadUtils {
    
    private final String TAG = "LogUploadUtils";
    private final String BUCKET_NAME = "huadao-protocal-data";
    private static volatile LogUploadUtils mInstance;
    
    private BosClient mBosClient;
    
    private LogUploadUtils(){
    }
    
    
    public static LogUploadUtils getInstance(){
        if (mInstance == null) {
            synchronized (LogUploadUtils.class) {
                if (mInstance == null) {
                    mInstance = new LogUploadUtils();
                }
            }
        }
        return mInstance;
    }
    
    
    /**
     * 上传日志
     *
     * @param context
     */
    public void uploadLogData(final Context context){
        new AsyncTask<Void,Void,Void>() {
            @Override
            protected Void doInBackground(Void... params){
                if (!initBDC(context)) {//判断百度云是否初始化
                    Log.i(TAG, "百度云未初始化，不能上传日志");
                    return null;
                }
                File[] logFiles = LoggerUtils.getAllLogFiles(context);
                if (null == logFiles || logFiles.length < 1) {
                    return null;
                }
                for (File logFile : logFiles) {
                    if (logFile.exists() && logFile.length() > 0) {
                        doLogUpload(logFile);
                    }
                }
                return null;
            }
        }.execute();
    }
    
    
    /**
     * 执行上传动作
     *
     * @param f
     */
    private void doLogUpload(File f){
        Log.v(TAG, "doLogUpload, 上传文件: " + f.getAbsolutePath());
        final String fileName = f.getName();//文件名
        Log.v(TAG, "doLogUpload, objectKey: " + fileName);
        try {
            PutObjectResponse response = mBosClient.putObject(BUCKET_NAME, fileName, f);
            final String etag = response.getETag();
            Log.v(TAG, "doLogUpload, etag: " + etag);
            if (!TextUtils.isEmpty(etag)) {
                Log.v(TAG, "doLogUpload, 上传成功,即将删除");
                f.delete();
            }
        } catch (BceServiceException e) {
            Log.e(TAG, "doLogUpload Error ErrorCode: " + e.getErrorCode());
            Log.e(TAG, "doLogUpload Error RequestId: " + e.getRequestId());
            Log.e(TAG, "doLogUpload Error StatusCode: " + e.getStatusCode());
            Log.e(TAG, "doLogUpload Error Message: " + e.getMessage());
            Log.e(TAG, "doLogUpload Error ErrorType: " + e.getErrorType());
        } catch (BceClientException e) {
            Log.e(TAG, "Error Message: " + e.getMessage());
        }
    }
    
    /**
     * 初始化百度云上传服务
     *
     * @param context
     * @return
     */
    private boolean initBDC(Context context){
        try {
            if (null == mBosClient) {
                String ACCESS_KEY = context.getString(R.string.bos_access_key);
                String SECRET_KEY = context.getString(R.string.bos_secret_key);
                String END_POINT = context.getString(R.string.bos_end_point);
                BosClientConfiguration configuration = new BosClientConfiguration();
                configuration.setCredentials(new DefaultBceCredentials(ACCESS_KEY, SECRET_KEY));
                configuration.setEndpoint(END_POINT);
                mBosClient = new BosClient(configuration);
            }
            
            final boolean exists = mBosClient.doesBucketExist(BUCKET_NAME);
            if (!exists) {
                mBosClient.createBucket(BUCKET_NAME);
            }
        } catch (BceServiceException e) {
            Log.e(TAG, "initBDC Error ErrorCode: " + e.getErrorCode());
            Log.e(TAG, "initBDC Error RequestId: " + e.getRequestId());
            Log.e(TAG, "initBDC Error StatusCode: " + e.getStatusCode());
            Log.e(TAG, "initBDC Error Message: " + e.getMessage());
            Log.e(TAG, "initBDC Error ErrorType: " + e.getErrorType());
            return false;
        } catch (BceClientException e) {
            Log.e(TAG, "initBDC Error Message: " + e.getMessage());
            return false;
        }
        return true;
    }

    //LogUploadUtils.getInstance().uploadLogData(getApplicationContext());
}